package com.sskj.login.http;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.LanguageUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.util.CommonUtil;
import com.sskj.login.bean.LoginBean;

import java.util.Locale;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-15 14:08
 */
public class HttpService {


    /**
     * 发送验证码（注册）
     *
     * @param  mcode 区号
     * @param mobile 手机号
     * @return
     */
    public PostRequest<HttpData> sendSms(String mcode, String mobile) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL+"/api/user/sendSms")
                .params("area_code", mcode)
                .params("phone", mobile)
                .params("from", "register");
    }

    /**
     * 发送邮件
     *
     * @param email 邮箱号
     * @return
     */
    public PostRequest<HttpData> sendEmail(String email, String type) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SEND_EMAIL)
                .params("email", email)
                .params("type", type)
                ;
    }

    /**
     * 注册
     *
     * @param mobile 手机号/邮箱号
     * @param code   验证码
     * @param opwd   密码
     * @param opwd1  重复密码
     * @param tjuser 推荐码/邀请码
     * @return
     */
    public PostRequest<HttpData> register(String mcode, String mobile, String code, String opwd, String opwd1, String tjuser) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.REGISTER)
                .params("phone", mobile)
                .params("code", code)
                .params("area_code", mcode.replace("+", ""))
                .params("password", CommonUtil.getFiveMd5Value(opwd))
                .params("c_password", CommonUtil.getFiveMd5Value(opwd1))
                .params("recommend", tjuser);
    }

    /**
     * 登录
     *
     * @param mobile 手机号/邮箱号
     * @param opwd   密码
     * @return
     */
    public PostRequest<HttpData<LoginBean>> login(String mcode, String mobile, String opwd, String google_code, String secret) {
        return OkGo.<HttpData<LoginBean>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                .params("mcode", mcode)
                .params("phone", mobile)
                .params("google_code", google_code)
                .params("secret", secret)
                .params("password", CommonUtil.getFiveMd5Value(opwd));
    }

    /**
     * 重置登录密码
     *
     * @param mobile 手机号/邮箱号
     * @param opwd   密码
     * @param opwd1  确认登录密码
     * @param code   验证码
     * @return
     */
    public PostRequest<HttpData> resetLoginPwd(String mcode, String mobile, String opwd, String opwd1, String code) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.RESET_LOGIN_PWD)
                .params("mcode", mcode)
                .params("phone", mobile)
                .params("password", CommonUtil.getFiveMd5Value(opwd))
                .params("password_confirmation", CommonUtil.getFiveMd5Value(opwd1))
                .params("code", code);
    }


    public PostRequest<HttpData> checkGoogle(String code) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CHECK_GOOGLE)
                .params("dyGoodleCommand", code)
                ;
    }


    public PostRequest<HttpData> checkCode(String account, String code) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CHECK_CODE)
                .params("mobile", account)
                .params("code", code);

    }


    public PostRequest<HttpData<LoginBean>> loginCode(String account, String code) {
        return OkGo.<HttpData<LoginBean>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN_CODE)
                .params("mobile", account)
                .params("code", code)
                ;
    }

    public PostRequest<HttpData<String>> getXieyi(String xieyi) {
        return OkGo.<HttpData<String>>post(HttpConfig.BASE_URL + HttpConfig.XIEYI)
                .params("type", xieyi + (LanguageUtil.getAppLocale(App.INSTANCE) == Locale.ENGLISH ? "1" : ""))
                ;
    }
}